package com.googlecode.cswish.security.validator;

import com.googlecode.cswish.security.auth.Parameter;
import com.googlecode.cswish.security.type.Expression;

/**
 * @author Jerry.Feng Date: Nov 8, 2007 1:52:36 PM
 * @version $id: $
 */
public class Equals implements Expression{

	private String paramName;
	
	private String value;
	
	public Equals(String paramName, String value) {
		this.paramName = paramName;
		this.value = value;
	}
	
	public boolean execute(Parameter parameter) {
		String theValue = parameter.getParameter(paramName);
		return value.equals(theValue);
	}

	public boolean implies(Expression expression) {
		if (expression != null && expression.getClass().equals(Expression.class)) {
			Equals equals = (Equals) expression;
			return equals(this.paramName, equals.paramName) 
				&& equals(this.value, equals.value);
		}
		return false;
	}
	
	private boolean equals(String v1, String v2) {
		return v1 == null && v2 == null || v1 != null && v1.equals(v2);
	}
}